/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.provider.vo.basic;

import com.inspur.edp.cef.spi.jsonser.base.StringUtils;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.sgf.api.entity.ServiceOperation;
import com.inspur.edp.sgf.api.entity.ServiceParameter;
import com.inspur.edp.sgf.api.entity.SgModelType;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: SimpleBasicActionProvider
 * @Author: Fynn Qi
 * @Date: 2020/7/31 10:42
 * @Version: V1.0
 */
public class SimpleBasicActionProvider {

    public static List<ServiceOperation> getSimpleBasicActions(GspMetadata gspMetadata) {
        List<ServiceOperation> operations = new ArrayList<>();
        //增加
        operations.add(create(gspMetadata));
        //删除
        operations.add(delete(gspMetadata));
        //批量删除
        operations.add(batchDelete(gspMetadata));
        //保存
        operations.add(update(gspMetadata));
        //查询
        operations.add(retrieve(gspMetadata));
        //过滤查询
        operations.add(query(gspMetadata));
        //增加默认值
        operations.add(createDefaultValue(gspMetadata));
        return operations;
    }


    private static ServiceOperation query(GspMetadata vmMetadata) {
        GspViewModel viewModel = (GspViewModel) vmMetadata.getContent();
        ServiceOperation operation = new ServiceOperation();

        operation.setId(MessageFormat.format("{0}&^^&Query", vmMetadata.getHeader().getId()));
        operation.setCode("Query");
        operation.setName("查询" + viewModel.getMainObject().getName());
        operation.setDescription(MessageFormat.format("查询{0}，可指定查询过滤条件、排序字段、设置分页。",
            viewModel.getMainObject().getName()));
        operation.setResourceOpId(MessageFormat.format("{0}&^^&Query", vmMetadata.getHeader().getId()));
        operation.setHttpMethod("GET");
        operation.setTemplate("");
        operation.setDeprecated(false);
        operation.setExtend1("Basic");

        operation.setParameters(new ArrayList<>());

        ServiceParameter parameter1 = new ServiceParameter();
        parameter1.setId(
                MessageFormat.format("{0}&^^&entityFilter", vmMetadata.getHeader().getId()));
        parameter1.setCode("entityFilter");
        parameter1.setName("过滤条件");
        parameter1.setDescription("过滤条件");
        parameter1.setRetVal(false);
        parameter1.setBasicType(true);
        parameter1.setCollection(false);
        parameter1.setListDeepth(0);
        parameter1.setModelId("string");
        parameter1.setModelName("string");
        parameter1.setModelType(SgModelType.VO);
        parameter1.setIn("Query");
        parameter1.setIsRequired(true);
        operation.getParameters().add(parameter1);

        ServiceParameter parameter2 = new ServiceParameter();
        parameter2.setId(MessageFormat.format("{0}&^^&{1}", vmMetadata.getHeader().getId(), StringUtils.toCamelCase(viewModel.getMainObject().getCode())));
        parameter2.setCode(StringUtils.toCamelCase(viewModel.getMainObject().getCode()));
        parameter2.setName(viewModel.getMainObject().getName());
        parameter2.setDescription("实体模型");
        parameter2.setRetVal(true);
        parameter2.setBasicType(false);
        parameter2.setCollection(true);
        parameter2.setListDeepth(1);
        parameter2.setModelId(vmMetadata.getHeader().getId());
        parameter2.setModelCode(vmMetadata.getHeader().getCode());
        parameter2.setModelName(vmMetadata.getHeader().getName());
        parameter2.setModelType(SgModelType.VO);
        parameter2.setRefId(viewModel.getMainObject().getId());
        parameter2.setRefCode(viewModel.getMainObject().getCode());
        parameter2.setRefName(viewModel.getMainObject().getName());
        parameter2.setIn("");
        parameter2.setIsRequired(true);
        operation.getParameters().add(parameter2);

        return operation;
    }

    private static ServiceOperation retrieve(GspMetadata vmMetadata) {
        GspViewModel viewModel = (GspViewModel) vmMetadata.getContent();
        ServiceOperation operation = new ServiceOperation();

        operation.setId(MessageFormat.format("{0}&^^&Retrieve", vmMetadata.getHeader().getId()));
        operation.setCode("Retrieve");
        operation.setName("检索" + viewModel.getMainObject().getName());
        operation.setDescription(MessageFormat.format("检索指定的{0}，返回结果中包含{0}的所有子表数据。", viewModel.getMainObject().getName()));
        operation.setResourceOpId(
                MessageFormat.format("{0}&^^&Retrieve", vmMetadata.getHeader().getId()));
        operation.setHttpMethod("GET");
        operation.setTemplate("/{dataId}");
        operation.setDeprecated(false);
        operation.setExtend1("Basic");

        operation.setParameters(new ArrayList<>());

        ServiceParameter param1 = new ServiceParameter();
        param1.setId(MessageFormat.format("{0}&^^&dataId", vmMetadata.getHeader().getId()));
        param1.setCode("dataId");
        param1.setName("单据内码");
        param1.setDescription("单据内码");
        param1.setRetVal(false);
        param1.setBasicType(true);
        param1.setCollection(false);
        param1.setListDeepth(0);
        param1.setModelId("string");
        param1.setModelName("string");
        param1.setModelType(SgModelType.VO);
        param1.setIn("Path");
        param1.setIsRequired(true);
        operation.getParameters().add(param1);

        ServiceParameter param2 = new ServiceParameter();
        param2.setId(MessageFormat.format("{0}&^^&{1}", vmMetadata.getHeader().getId(), StringUtils.toCamelCase(viewModel.getMainObject().getCode())));
        //code要做ToCamel处理
        param2.setCode(StringUtils.toCamelCase(viewModel.getMainObject().getCode()));
        param2.setName(viewModel.getMainObject().getName());
        param2.setDescription("实体模型");
        param2.setRetVal(true);
        param2.setBasicType(false);
        param2.setCollection(false);
        param2.setListDeepth(0);
        param2.setModelId(vmMetadata.getHeader().getId());
        param2.setModelCode(vmMetadata.getHeader().getCode());
        param2.setModelName(vmMetadata.getHeader().getName());
        param2.setModelType(SgModelType.VO);
        param2.setRefId(viewModel.getMainObject().getId());
        param2.setRefCode(viewModel.getMainObject().getCode());
        param2.setRefName(viewModel.getMainObject().getName());
        param2.setIn("");
        param2.setIsRequired(true);
        operation.getParameters().add(param2);

        return operation;
    }

    private static ServiceOperation update(GspMetadata vmMetadata) {
        GspViewModel viewModel = (GspViewModel) vmMetadata.getContent();
        ServiceOperation operation = new ServiceOperation();

        operation.setId(MessageFormat.format("{0}&^^&Update", vmMetadata.getHeader().getId()));
        operation.setCode("Update");
        operation.setName("更新" + viewModel.getMainObject().getName());
        operation.setDescription(MessageFormat.format("修改指定的{0}，传入值必须包含完整的{0}数据。", viewModel.getMainObject().getName()));
        operation.setResourceOpId(
                MessageFormat.format("{0}&^^&Update", vmMetadata.getHeader().getId()));
        operation.setHttpMethod("PUT");
        operation.setTemplate("");
        operation.setDeprecated(false);
        operation.setExtend1("Basic");

        operation.setParameters(new ArrayList<>());


        ServiceParameter param = new ServiceParameter();
        param.setId(MessageFormat.format("{0}&^^&{1}", vmMetadata.getHeader().getId(), StringUtils.toCamelCase(viewModel.getMainObject().getCode())));
        param.setCode(StringUtils.toCamelCase(viewModel.getMainObject().getCode()));
        param.setName(viewModel.getMainObject().getName());
        param.setDescription(viewModel.getMainObject().getDescription());
        param.setRetVal(false);
        param.setBasicType(false);
        param.setCollection(false);
        param.setListDeepth(0);
        param.setModelId(vmMetadata.getHeader().getId());
        param.setModelCode(vmMetadata.getHeader().getCode());
        param.setModelName(vmMetadata.getHeader().getName());
        param.setModelType(SgModelType.VO);
        param.setRefId(viewModel.getMainObject().getId());
        param.setRefCode(viewModel.getMainObject().getCode());
        param.setRefName(viewModel.getMainObject().getName());
        param.setIn("Body");
        param.setIsRequired(true);
        operation.getParameters().add(param);

        ServiceParameter retParam = new ServiceParameter();
        retParam.setId(MessageFormat.format("{0}&^^&retParam", vmMetadata.getHeader().getId()));
        retParam.setCode("return");
        retParam.setName("返回值");
        retParam.setDescription("返回值");
        retParam.setRetVal(true);
        retParam.setBasicType(false);
        retParam.setCollection(false);
        retParam.setListDeepth(0);
        retParam.setModelId(vmMetadata.getHeader().getId());
        retParam.setModelCode(vmMetadata.getHeader().getCode());
        retParam.setModelName(vmMetadata.getHeader().getName());
        retParam.setModelType(SgModelType.VO);
        retParam.setRefId(viewModel.getMainObject().getId());
        retParam.setRefCode(viewModel.getMainObject().getCode());
        retParam.setRefName(viewModel.getMainObject().getName());
        retParam.setIn("");
        retParam.setIsRequired(true);
        operation.getParameters().add(retParam);

        return operation;
    }

    private static ServiceOperation batchDelete(GspMetadata vmMetadata) {
        GspViewModel viewModel = (GspViewModel) vmMetadata.getContent();
        ServiceOperation operation = new ServiceOperation();

        operation.setId(MessageFormat.format("{0}&^^&BatchDelete", vmMetadata.getHeader().getId()));
        operation.setCode("BatchDelete");
        operation.setName("批量删除" + viewModel.getMainObject().getName());
        operation.setDescription(MessageFormat.format("批量删除指定的{0}。", viewModel.getMainObject().getName()));
        operation.setResourceOpId(
                MessageFormat.format("{0}&^^&BatchDelete", vmMetadata.getHeader().getId()));
        operation.setHttpMethod("DELETE");
        operation.setTemplate("");
        operation.setDeprecated(false);
        operation.setExtend1("Basic");

        operation.setParameters(new ArrayList<>());

        ServiceParameter param = new ServiceParameter();
        param.setId(
                MessageFormat.format("{0}&^^&ids", vmMetadata.getHeader().getId()));
        param.setCode("ids");
        param.setName("单据内码列表");
        param.setDescription("单据内码列表");
        param.setRetVal(false);
        param.setBasicType(true);
        param.setCollection(false);
        param.setListDeepth(0);
        param.setModelId("string");
        param.setModelName("string");
        param.setModelType(SgModelType.VO);
        param.setIn("Query");
        param.setIsRequired(true);
        operation.getParameters().add(param);

        return operation;
    }

    private static ServiceOperation delete(GspMetadata vmMetadata) {
        GspViewModel viewModel = (GspViewModel) vmMetadata.getContent();
        ServiceOperation operation = new ServiceOperation();

        operation.setId(MessageFormat.format("{0}&^^&Delete", vmMetadata.getHeader().getId()));
        operation.setCode("Delete");
        operation.setName("删除" + viewModel.getMainObject().getName());
        operation.setDescription(MessageFormat.format("删除指定的{0}。", viewModel.getMainObject().getName()));
        operation.setResourceOpId(
                MessageFormat.format("{0}&^^&Delete", vmMetadata.getHeader().getId()));
        operation.setHttpMethod("DELETE");
        operation.setTemplate("/{dataId}");
        operation.setDeprecated(false);
        operation.setExtend1("Basic");

        operation.setParameters(new ArrayList<>());

        ServiceParameter param = new ServiceParameter();
        param.setId(MessageFormat.format("{0}&^^&dataId", vmMetadata.getHeader().getId()));
        param.setCode("dataId");
        param.setName("单据内码");
        param.setDescription("单据内码");
        param.setRetVal(false);
        param.setBasicType(true);
        param.setCollection(false);
        param.setListDeepth(0);
        param.setModelId("string");
        param.setModelName("string");
        param.setModelType(SgModelType.VO);
        param.setIn("Path");
        param.setIsRequired(true);
        operation.getParameters().add(param);

        return operation;
    }

    private static ServiceOperation create(GspMetadata vmMetadata) {
        GspViewModel viewModel = (GspViewModel) vmMetadata.getContent();
        ServiceOperation operation = new ServiceOperation();

        operation.setId(MessageFormat.format("{0}&^^&Create", vmMetadata.getHeader().getId()));
        operation.setCode("Create");
        operation.setName("新增" + viewModel.getMainObject().getName());
        operation.setDescription(MessageFormat.format("新增{0}，可指定字段初始值。", viewModel.getMainObject().getName()));
        operation.setResourceOpId(
                MessageFormat.format("{0}&^^&Create", vmMetadata.getHeader().getId()));
        operation.setHttpMethod("POST");
        operation.setTemplate("");
        operation.setDeprecated(false);
        operation.setExtend1("Basic");

        operation.setParameters(new ArrayList<>());

        ServiceParameter param = new ServiceParameter();
        param.setId(MessageFormat.format("{0}&^^&{1}", vmMetadata.getHeader().getId(), StringUtils.toCamelCase(viewModel.getMainObject().getCode())));
        param.setCode(StringUtils.toCamelCase(viewModel.getMainObject().getCode()));
        param.setName(viewModel.getMainObject().getName());
        param.setDescription(viewModel.getMainObject().getDescription());
        param.setRetVal(false);
        param.setBasicType(false);
        param.setCollection(false);
        param.setListDeepth(0);
        param.setModelId(vmMetadata.getHeader().getId());
        param.setModelCode(vmMetadata.getHeader().getCode());
        param.setModelName(vmMetadata.getHeader().getName());
        param.setModelType(SgModelType.VO);
        param.setRefId(viewModel.getMainObject().getId());
        param.setRefCode(viewModel.getMainObject().getCode());
        param.setRefName(viewModel.getMainObject().getName());
        param.setIn("Body");
        param.setIsRequired(true);
        operation.getParameters().add(param);


        ServiceParameter retParam = new ServiceParameter();
        retParam.setId(MessageFormat.format("{0}&^^&retParam", vmMetadata.getHeader().getId()));
        retParam.setCode("return");
        retParam.setName("返回值");
        retParam.setDescription("返回值");
        retParam.setRetVal(true);
        retParam.setBasicType(false);
        retParam.setCollection(false);
        retParam.setListDeepth(0);
        retParam.setModelId(vmMetadata.getHeader().getId());
        retParam.setModelCode(vmMetadata.getHeader().getCode());
        retParam.setModelName(vmMetadata.getHeader().getName());
        retParam.setModelType(SgModelType.VO);
        retParam.setRefId(viewModel.getMainObject().getId());
        retParam.setRefCode(viewModel.getMainObject().getCode());
        retParam.setRefName(viewModel.getMainObject().getName());
        retParam.setIn("");
        retParam.setIsRequired(true);
        operation.getParameters().add(retParam);

        return operation;
    }

    private static ServiceOperation createDefaultValue(GspMetadata vmMetadata) {
        GspViewModel viewModel = (GspViewModel) vmMetadata.getContent();
        ServiceOperation operation = new ServiceOperation();

        operation.setId(MessageFormat.format("{0}&^^&CreateDefaultValue", vmMetadata.getHeader().getId()));
        operation.setCode("CreateDefaultValue");
        operation.setName("新增" + viewModel.getMainObject().getName());
        operation.setDescription("新增" + viewModel.getMainObject().getName());
        operation.setResourceOpId(
                MessageFormat.format("{0}&^^&CreateDefaultValue", vmMetadata.getHeader().getId()));
        operation.setHttpMethod("PUT");
        operation.setTemplate("/service/defaultvalue");
        operation.setDeprecated(false);
        operation.setExtend1("Basic");

        operation.setParameters(new ArrayList<>());

        ServiceParameter retParam = new ServiceParameter();
        retParam.setId(MessageFormat.format("{0}&^^&retParam", vmMetadata.getHeader().getId()));
        retParam.setCode("return");
        retParam.setName("返回值");
        retParam.setDescription("返回值");
        retParam.setRetVal(true);
        retParam.setBasicType(false);
        retParam.setCollection(false);
        retParam.setListDeepth(0);
        retParam.setModelId(vmMetadata.getHeader().getId());
        retParam.setModelCode(vmMetadata.getHeader().getCode());
        retParam.setModelName(vmMetadata.getHeader().getName());
        retParam.setModelType(SgModelType.VO);
        retParam.setRefId(viewModel.getMainObject().getId());
        retParam.setRefCode(viewModel.getMainObject().getCode());
        retParam.setRefName(viewModel.getMainObject().getName());
        retParam.setIn("");
        retParam.setIsRequired(true);
        operation.getParameters().add(retParam);

        return operation;
    }
}
